#!/bin/bash

export TOP=`pwd`
export CommonPath=$TOP/Shell/CommonFunction

HELPFILE=$TOP/Files/shellhelp.txt
#导入环境配置（颜色等）
source ./Shell/EnvironmentConfiguration.sh

My_Error(){
	echo -e "${MYERROR}"
	if [[ $1 == My_ENP ]];then
		if [[ $2 == My_ENLP ]];then  #参数数目过少
			echo "参数数目过少！"
			echo "参数数目过多！"
		elif [[ $2 == My_ENMP ]];then  #参数数目过多
			echo "参数数目过多！"
		else
			echo "参数数目错误！"
		fi
	fi
	echo -e "${MYRESET}"
}

My_AddBuildrootApp(){  #新建buildroot工程，不检查错误。调用格式：My_AddBuildrootApp Project_Name
	source $TOP/Shell/SpecialFunction/BuildRoot.sh
	My_AddBuildRoot
}
My_RemoveBuildrootApp(){  #新建buildroot工程，不检查错误。调用格式：My_AddBuildrootApp Project_Name
	source $TOP/Shell/SpecialFunction/BuildRoot.sh
	My_RmBuildRoot $1      #My_RmBuildRoot必须输入参数（防止误操作）
}

: <<EOF
My_CpBuildrootApp(){ #将相关文件修改，并拷贝到指定位置
	My_CpBuildRoot $1
}
EOF

export Project_Name     #项目名称
export Exe_Name         #exe名称
if [ $# -eq 0 ];then
	echo -e "${MYHELP_1}"
	echo "---------------mainshell调用格式：-------------------"
	echo -e "${MYHELP_2}"
	echo "mainshell.sh help：打印mainshell.sh帮助信息"
	echo "mainshell.sh AddBuildrootApp：创建Buildroot APP相关操作"
	echo "mainshell.sh AddBuildrootApp ProjectName ExeName"
	echo -e "${MYHELP_3}"
	echo "mainshell.sh RemoveBuildrootApp：删除Buildroot APP相关操作"
	echo "mainshell.sh RemoveBuildrootApp help"
	echo "mainshell.sh RemoveBuildrootApp ProjectName"
	echo "mainshell.sh RemoveBuildrootApp all"
	echo -e "${MYHELP_2}"
	echo "mainshell.sh UpBuildrootApp：更新Buildroot APP相关操作"
	echo "mainshell.sh UpBuildrootApp ProjectName"

	echo -e "${MYHELP_1}"
	echo "-----------------------------------------------------"
	echo -e "${MYRESET}"
elif [ $# -ge 1 ];then
	echo -e "$1,$2,$3"
	if [ $1 == help ];then
		cat $HELPFILE
	elif [ $1 == AddBuildrootApp ];then
		if [ $# -eq 2 ];then #如果只有两个参数,默认exe=projec
			Project_Name=$2
			Exe_Name=$2
			My_AddBuildrootApp
		elif [ $# -eq 3 ];then
			Project_Name=$2
			Exe_Name=$3
			My_AddBuildrootApp
		else
			My_Error My_ENP
		fi
	elif [ $1 == RemoveBuildrootApp ];then
		if [ $# -eq 2 ];then #如果只有两个参数
			if [[ $2 == help ]];then
				ls -l $TOP/config | grep '^d' | awk '{print $NF}'
			else
				Project_Name=$2
				My_RemoveBuildrootApp $2
			fi
		else
			My_Error My_ENP
		fi
	elif [ $1 == RunMainApp ];then  #必须传入SDK路径
		echo "RunMainApp1"
		pwd
		cd $2
		echo "RunMainApp2"
		pwd
		make MainApp-dirclean
		make MainApp
		cd -
		pwd
		echo "RunMainApp3"
	fi
fi

exit 0








: <<EOF
	elif [[ $1 == AddBuildrootApp ]];then
		cat $HELPFILE
#创建文件夹函数
My_Mkdir(){
	echo "----------$8"
	#mkdir -p "$1"
}
#删除文件夹函数
My_MkdirClean(){
	echo "$2"
	#rm -rf "$1"
}

#拷贝文件函数
My_FileCp(){
	echo "***My_FileCp"
}

#创建app的主函数(输入工程名，自动创建SDK所需文件夹、修正更新Makefile及相关文件)
Buildroot_SDK_PathNum=" " #SDK路径和

My_BuildrootApp(){
if [[ $1 == AppMkdir ]]; then   #创建文件夹，输入格式：AppMkdir，PATH（带文件夹名字）
	if [ $# -eq 2 ]; then   #必须输入Project_Name
		My_Mkdir $2
	fi
elif [[ $1 == AppMkdirClean ]]; then #删除文件夹，输入格式：AppMkdirClean，PATH（带文件夹名字）

fi
}



if [ $# -eq 0 ]; then
	echo -e "${RED} ${BBLUE}"
	echo "---------------mainshell调用格式：-------------------"
	echo -e "${BLUE} ${BWHITE}"
	echo "mainshell.sh BuildrootApp：创建Buildroot APP相关操作"
	echo -e "${RED} ${BBLUE}"
	echo "-----------------------------------------------------"
	echo -e "${RESET}"
else
	if [[ $1 == BuildrootApp ]]; then
		if [ $# -eq 1 ]; then  #构建buildrootapp必须输入：BuildrootApp、项目名称
			echo -e "${RED} ${BBLUE}"
			echo "---------------BuildrootApp调用格式：-------------------"
			echo -e "${BLUE} ${BWHITE}"
			echo "mainshell.sh BuildrootApp AppMkdir Project_Name"
			echo -e "${RED} ${BBLUE}"
			echo "-----------------------------------------------------"
			echo -e "${RESET}"
		elif [[ $2 == AppMkdir ]]; then
			if [ $# -eq 2 ]; then
				echo -e " ${ERROR_COLOR} Please Input Project_Name! ${RESET}"
				exit -1
			else
				My_BuildrootApp AppMkdir $3
			fi

		fi
	fi
fi



	主shell进程！
	以下是备用信息：
#if [ $# -eq 1 ]; then
#	Myerror
#fi

My_BuildrootApp ${@:2}

echo -e "${YELLOW} ${BRED} ZAZ ${RESET} NN ${RESET}"

echo " 输入参数个数错误 $0 : $# !"

if [ $# -lt 3 ];
if [ $# -gt 2 ];

	count_num=0
	for ((i=2;i<=$#;i++)); do
		eval "mkdirpath=\${$i}"
		Buildroot_SDK_PathNum+="$mkdirpath "
		let count_num+=1
	done

EOF
